<template>
  <div class="layout-parent w100 p10">
    <el-tabs type="border-card">
      <el-tab-pane>
        <template #label><vp-label zh="基本信息" en="Basic Info"/></template>
        <DeptRectification :id="businessId" :type="'feedback'" ref="taskRef" @dataEmits="getData"></DeptRectification>
      </el-tab-pane>
      <el-tab-pane>
        <template #label><vp-label zh="流程记录" en="Process Record"/></template>
        <ProcessHistory :processInsId="processInsId" />
      </el-tab-pane>
      <el-tab-pane>
        <template #label><vp-label zh="流程图" en="Process Chart"/></template>
        <ProcessView :processInsId="processInsId" />
      </el-tab-pane>
    </el-tabs>
    <StatusBar
      :id="formData.id"
      :status="formData.zt"
      :isValid="formData.yxbs"
    />
    <PageBottom>
      <VpProcessToolBar 
        :process-ins-id="processInsId" 
        :task-id="taskId" 
        :loading="submitLoading" 
        :before-show-fn="beforeShowFn"
        :after-agree-fn="afterAgreeFn"
        :set-loading-fn="(_loading: boolean) => { submitLoading = _loading }"
      />
    </PageBottom>
  </div>
</template>
<script lang="ts">
import {
  defineComponent,
  getCurrentInstance,
  onMounted,
  reactive,
  ref,
  toRefs,
} from "vue";
import request from "/@/utils/request";
import ProcessHistory from '/@/components/vp/workflow/ProcessHistory.vue';
import ProcessView from '/@/components/vp/workflow/ProcessView.vue';
import ProcessHandle from "/@/components/vp/workflow/VpProcessHandle.vue";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import DeptRectification from "./DeptRectification.vue";
import PageBottom from '/@/components/vp/PageBottom/index.vue';
import mittBus from '/@/utils/mitt';
import StatusBar from '/@/components/vp/status/StatusBar.vue';
import VpProcessToolBar from '/@/components/vp/workflow/VpProcessToolBar.vue';

export default defineComponent({
  name: "feedbackDeptRectification2",
  props: {
    businessId: String,
    taskId: String,
    processInsId: String,
  },
  components: { ProcessHistory, ProcessView, ProcessHandle, DeptRectification, PageBottom, StatusBar, VpProcessToolBar },
  setup(props) {
    const state = reactive({
      loading: false,
      submitLoading: false,
      type: "feedback",
      formData: {
        id: "",
        zt: 10,
        yxbs: 1,
        processHandle: {},
      },
      isAgree: true,
    });

    const loadWorkflow = () => {};
    onMounted(() => {
      loadWorkflow();
    });
    const { proxy } = getCurrentInstance() as any;
    const route = useRoute();
    const processHandleRef = ref();
    const taskRef = ref();

    const getData = (data: any) => {
      state.formData = data;
    };

    const handleOperateChanged = (operate: boolean)=>{
        state.isAgree = operate;
    }

    //同意前操作
    const beforeShowFn = () => {
      state.submitLoading = true;
      return taskRef.value.getData().finally(()=>{state.submitLoading = false;});
    }
    
    //同意后操作
    const afterAgreeFn = (processHandle: any) => { 
      state.submitLoading = true;
      taskRef.value.getData(88).then((data: any) =>{
        request.post("/safe/deptRectification/feedback", {
            ...data,
            processHandle: {
                processInsId: props.processInsId,
                taskId: props.taskId,
                ...processHandle,
            }
        }).then(()=>{
            mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
            ElMessage.success("办理完毕/Completed Processing");
        }).catch(()=>{
        }).finally(()=>{
            state.submitLoading = false;
        })
      }).catch(()=>{state.submitLoading = false;});
    }

    return {
      ...toRefs(state),
      ...toRefs(props),
      processHandleRef,
      taskRef,
      getData,
      handleOperateChanged,
      beforeShowFn,
      afterAgreeFn,
    };
  },
});
</script>